/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HospedagemEditor.java
 *
 * Created on Apr 9, 2009, 10:36:14 AM
 */

package doghost.editors;

import doghost.controllers.ItemDeHospedagemController;
import doghost.entities.Hospedagem;
import doghost.entities.ItemDeHospedagem;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jdesktop.application.Action;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.beansbinding.BindingGroup;

/**
 *
 * @author duo
 */
public class HospedagemEditor extends javax.swing.JDialog {

    protected Hospedagem currentRecord;
    protected ItemDeHospedagem itemDeHospedagem;
    protected List<ItemDeHospedagem> removedItensDeHospedagem;
    protected boolean saveFired;
    protected boolean itemsChanged;
    private PropertyChangeSupport propertyChangeSupport =
        new PropertyChangeSupport(this);

    /** Creates new form HospedagemEditor */
    public HospedagemEditor(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
    * Get the value of currentRecord
    *
    * @return the value of currentRecord
    */
    public Hospedagem getCurrentRecord() {
        return currentRecord;
    }

    /**
    * Set the value of currentRecord
    *
    * @param currentRecord new value of currentRecord
    */
    public void setCurrentRecord(Hospedagem currentRecord) {
        this.setCurrentRecord(currentRecord, false);
    }

    public void setCurrentRecord(Hospedagem currentRecord, boolean force) {
        Hospedagem oldRecord = this.currentRecord;
        if(force) {
            oldRecord = null;
        }
        this.currentRecord = currentRecord;
        propertyChangeSupport.firePropertyChange("currentRecord",
        oldRecord, currentRecord);
        itemDeHospedagemTable.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
            @Override
                public void valueChanged(ListSelectionEvent e) {
                        propertyChangeSupport.firePropertyChange("itemDeHospedagemRecordSelected",
                            !isItemDeHospedagemRecordSelected(), isItemDeHospedagemRecordSelected());
                    }
                });
    }

    /**
     * Get the value of itemDeHospedagem
     *
     * @return the value of itemDeHospedagem
     */
    public ItemDeHospedagem getItemDeHospedagem() {
        return itemDeHospedagem;
    }

    /**
     * Set the value of itemDeHospedagem
     *
     * @param itemDeHospedagem new value of itemDeHospedagem
     */
    public void setItemDeHospedagem(ItemDeHospedagem itemDeHospedagem) {
        ItemDeHospedagem oldItemDeHospedagem = this.itemDeHospedagem;
        this.itemDeHospedagem = itemDeHospedagem;
        propertyChangeSupport.firePropertyChange("itemDeHospedagem",
        oldItemDeHospedagem, itemDeHospedagem);
    }

    /**
     * Get the value of removedItensDeHospedagem
     *
     * @return the value of removedItensDeHospedagem
     */
    public List<ItemDeHospedagem> getRemovedItensDeHospedagem() {
        return removedItensDeHospedagem;
    }

    /**
     * Set the value of removedItensDeHospedagem
     *
     * @param removedItensDeHospedagem new value of removedItensDeHospedagem
     */
    public void setRemovedItensDeHospedagem(List<ItemDeHospedagem> removedItensDeHospedagem) {
        this.removedItensDeHospedagem = removedItensDeHospedagem;
    }

    public boolean isItemDeHospedagemRecordSelected() {
        return itemDeHospedagemTable.getSelectedRow() != -1;
    }

    /**
    * Get the value of saveFired
    *
    * @return the value of saveFired
    */
    public boolean isSaveFired() {
        return saveFired;
    }

    /**
    * Set the value of saveFired
    *
    * @param saveFired new value of saveFired
    */
    public void setSaveFired(boolean saveFired) {
        this.saveFired = saveFired;
    }

    /**
     * Get the value of itemsChanged
     *
     * @return the value of itemsChanged
     */
    public boolean isItemsChanged() {
        return itemsChanged;
    }

    /**
     * Set the value of itemsChanged
     *
     * @param itemsChanged new value of itemsChanged
     */
    public void setItemsChanged(boolean itemsChanged) {
        this.itemsChanged = itemsChanged;
    }

    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    @Override
    public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    @Action
    public void saveHospedagem() {
        setSaveFired(true);
        setVisible(false);
    }

    @Action
    public void cancelHospedagem() {
        setSaveFired(false);
        setVisible(false);
    }

    @Action(enabledProperty = "itemDeHospedagemRecordSelected")
    public void editItemDeHospedagem() {
        int index = itemDeHospedagemTable.getSelectedRow();
        ItemDeHospedagem selectedItemDeHospedagem = null;
        Iterator<ItemDeHospedagem> it =
                this.getCurrentRecord().getItensDeHospedagem().iterator();
        int i = 0;
        while(it.hasNext()) {
            selectedItemDeHospedagem = it.next();
            if(itemDeHospedagemTable.convertRowIndexToModel(index) == i) {
                break;
            } else {
                selectedItemDeHospedagem = null;
            }
            i++;
        }
        if(selectedItemDeHospedagem != null) {
            ItemDeHospedagemController itemDeHospedagemController =
                    new ItemDeHospedagemController();
            itemDeHospedagemController.setItemDeHospedagem((ItemDeHospedagem)
                    selectedItemDeHospedagem);
            itemDeHospedagemController.editWOPersistItemDeHospedagem();
            // se a operação foi bem sucedida
            if (itemDeHospedagemController.isOperationOk()) {
                this.setItemsChanged(true);
                Binding b = findBinding(bindingGroup, this,
                        this.itemDeHospedagemTable);
                if(b!=null) {
                    b.unbind();
                    rebindItemDeHospedagemTable(b);
                    //b.bind();
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Re-Binding realizado!");
                } else {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Binding não localizado!");
                }
                this.itemDeHospedagemTable.repaint();
            } else {
                Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Falha na operação de item de hospedagem.");
            }
        } else {
            Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Erro selecionando item de hospedagem.");
        }
    }


    @Action(enabledProperty = "itemDeHospedagemRecordSelected")
    public void deleteItemDeHospedagem() {
        int index = itemDeHospedagemTable.getSelectedRow();
        ItemDeHospedagem selectedItemDeHospedagem = null;
        Iterator<ItemDeHospedagem> it = this.getCurrentRecord().getItensDeHospedagem().iterator();
        int i = 0;
        while(it.hasNext()) {
            selectedItemDeHospedagem = it.next();
            if(itemDeHospedagemTable.convertRowIndexToModel(index) == i) {
                break;
            } else {
                selectedItemDeHospedagem = null;
            }
            i++;
        }
        if(selectedItemDeHospedagem != null) {
            ItemDeHospedagemController itemDeHospedagemController = new ItemDeHospedagemController();
            itemDeHospedagemController.setItemDeHospedagem(selectedItemDeHospedagem);
            itemDeHospedagemController.deleteWOPersistItemDeHospedagem();
            // se a operação foi bem sucedida
            if (itemDeHospedagemController.isOperationOk()) {
                this.setItemsChanged(true);
                Binding b = findBinding(bindingGroup, this,
                        this.itemDeHospedagemTable);
                if(b!=null) {
                    b.unbind();
                    if(this.removedItensDeHospedagem == null) {
                        this.removedItensDeHospedagem = new ArrayList<ItemDeHospedagem>();
                    }
                    this.removedItensDeHospedagem.add(selectedItemDeHospedagem);
                    this.getCurrentRecord().getItensDeHospedagem().remove(selectedItemDeHospedagem);
                    //b.bind();
                    rebindItemDeHospedagemTable(b);
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Re-Binding realizado!");
                } else {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Binding não localizado!");
                }
                this.itemDeHospedagemTable.repaint();
            }
        } else {
            Logger.getLogger(this.getClass().getName()).log(Level.WARNING,
                    "Erro selecionando item de hospedagem para eliminar.");
        }
    }

    @Action
    public void newItemDeHospedagem() {
        ItemDeHospedagemController itemDeHospedagemController = new ItemDeHospedagemController();
        this.setItemDeHospedagem(new ItemDeHospedagem());
        itemDeHospedagemController.setItemDeHospedagem(this.getItemDeHospedagem());
        itemDeHospedagemController.setHospedagem(this.getCurrentRecord());
        itemDeHospedagemController.newWOPersistItemDeHospedagem();
        // se a operação foi bem sucedida
        if (itemDeHospedagemController.isOperationOk()) {
            this.setItemsChanged(true);
            Binding b = findBinding(bindingGroup, this,
                    this.itemDeHospedagemTable);
            if (b != null) {
                b.unbind();
                if(this.getCurrentRecord().getItensDeHospedagem() == null) {
                    this.getCurrentRecord().setItensDeHospedagem(new ArrayList<ItemDeHospedagem> ());
                }
                this.getCurrentRecord().getItensDeHospedagem().add(
                        itemDeHospedagemController.getItemDeHospedagem());
                //b.bind();
                rebindItemDeHospedagemTable(b);
                Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Re-Binding realizado!");
            } else {
                Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Binding não localizado!");
            }
            this.itemDeHospedagemTable.repaint();
        } else {
            Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Falha na operação de item de hospedagem.");
        }
    }

    public Binding findBinding(BindingGroup bindingGroup, Object source, Object target) {
        for(Iterator<Binding> i = bindingGroup.getBindings().iterator(); i.hasNext(); ) {
            Binding b = i.next();
            boolean found = (source == null || b.getSourceObject() == source) &&
                    (target == null || b.getTargetObject() == target);
            if(found) {
                Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Binding source: "
                         + b.getSourceObject().toString());
                Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Binding target: "
                         + b.getTargetObject().toString());
                return b;
            }
        }
        return null;
    }

    private void rebindItemDeHospedagemTable(Binding b){
        try {
            org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${currentRecord.itensDeHospedagem}");
            org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, itemDeHospedagemTable);
            org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${servico}"));
            columnBinding.setColumnName("Servico");
            columnBinding.setColumnClass(doghost.entities.Servico.class);
            columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${quantidade}"));
            columnBinding.setColumnName("Quantidade");
            columnBinding.setColumnClass(Integer.class);
            columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${valor}"));
            columnBinding.setColumnName("Valor");
            columnBinding.setColumnClass(Double.class);
            columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${obs}"));
            columnBinding.setColumnName("Obs");
            columnBinding.setColumnClass(String.class);
            jTableBinding.setSourceUnreadableValue(java.util.Collections.emptyList());
            bindingGroup.addBinding(jTableBinding);
            jTableBinding.bind();
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getResourceMap(HospedagemEditor.class);
            itemDeHospedagemTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("itemDeHospedagemTable.columnModel.title0")); // NOI18N
            itemDeHospedagemTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("itemDeHospedagemTable.columnModel.title1")); // NOI18N
            itemDeHospedagemTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("itemDeHospedagemTable.columnModel.title2")); // NOI18N
            itemDeHospedagemTable.getColumnModel().getColumn(2).setCellRenderer(customCurrencyRenderer1);
            itemDeHospedagemTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("itemDeHospedagemTable.columnModel.title3")); // NOI18N
        } catch (Exception e) {
            Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Falha na operação de item de hospedagem.");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        doghostPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("doghostPU").createEntityManager();
        animalQuery = java.beans.Beans.isDesignTime() ? null : doghostPUEntityManager.createQuery("SELECT a FROM Animal a");
        animalList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : animalQuery.getResultList();
        customCurrencyRenderer1 = new doghost.utils.CustomCurrencyRenderer();
        localEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("doghostPU").createEntityManager();
        customFormattedDateHMConverter1 = new doghost.utils.CustomFormattedDateHMConverter();
        customFormattedDateConverter1 = new doghost.utils.CustomFormattedDateConverter();
        customFormattedDateValidator1 = new doghost.utils.CustomFormattedDateValidator();
        jLabel1 = new javax.swing.JLabel();
        animalComboBox = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        animalClienteNomeTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        animalClienteEnderecoTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        animalClienteFoneTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        animalClienteMobileTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        inicioFormattedTextField = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();
        fimFormattedTextField = new javax.swing.JFormattedTextField();
        itemDeHospedagemPanel = new org.jdesktop.swingx.JXPanel();
        itemDeHospedagemScrollPane = new javax.swing.JScrollPane();
        itemDeHospedagemTable = new javax.swing.JTable();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        newItemButton = new javax.swing.JButton();
        editItemButton = new javax.swing.JButton();
        deleteItemButton = new javax.swing.JButton();
        setFimHojeButton = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getResourceMap(HospedagemEditor.class);
        customCurrencyRenderer1.setText(resourceMap.getString("customCurrencyRenderer1.text")); // NOI18N
        customCurrencyRenderer1.setName("customCurrencyRenderer1"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        animalComboBox.setName("animalComboBox"); // NOI18N

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, animalList, animalComboBox);
        bindingGroup.addBinding(jComboBoxBinding);
        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.animal}"), animalComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        animalClienteNomeTextField.setEditable(false);
        animalClienteNomeTextField.setName("animalClienteNomeTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.animal.cliente.nome}"), animalClienteNomeTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        animalClienteEnderecoTextField.setEditable(false);
        animalClienteEnderecoTextField.setName("animalClienteEnderecoTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.animal.cliente.endereco}"), animalClienteEnderecoTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        animalClienteFoneTextField.setEditable(false);
        animalClienteFoneTextField.setName("animalClienteFoneTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.animal.cliente.fone}"), animalClienteFoneTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        animalClienteMobileTextField.setEditable(false);
        animalClienteMobileTextField.setName("animalClienteMobileTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.animal.cliente.mobile}"), animalClienteMobileTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        try {
            inicioFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/#### ##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        inicioFormattedTextField.setName("inicioFormattedTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.inicio}"), inicioFormattedTextField, org.jdesktop.beansbinding.BeanProperty.create("value"));
        binding.setConverter(customFormattedDateHMConverter1);
        binding.setValidator(customFormattedDateValidator1);
        bindingGroup.addBinding(binding);

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        try {
            fimFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/#### ##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        fimFormattedTextField.setName("fimFormattedTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.fim}"), fimFormattedTextField, org.jdesktop.beansbinding.BeanProperty.create("value"));
        binding.setConverter(customFormattedDateHMConverter1);
        bindingGroup.addBinding(binding);

        itemDeHospedagemPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("itemDeHospedagemPanel.border.title"))); // NOI18N
        itemDeHospedagemPanel.setName("itemDeHospedagemPanel"); // NOI18N

        itemDeHospedagemScrollPane.setName("itemDeHospedagemScrollPane"); // NOI18N

        itemDeHospedagemTable.setName("itemDeHospedagemTable"); // NOI18N

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${currentRecord.itensDeHospedagem}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, itemDeHospedagemTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${servico}"));
        columnBinding.setColumnName("Servico");
        columnBinding.setColumnClass(doghost.entities.Servico.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${quantidade}"));
        columnBinding.setColumnName("Quantidade");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${valor}"));
        columnBinding.setColumnName("Valor");
        columnBinding.setColumnClass(Double.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${obs}"));
        columnBinding.setColumnName("Obs");
        columnBinding.setColumnClass(String.class);
        jTableBinding.setSourceUnreadableValue(java.util.Collections.emptyList());
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        itemDeHospedagemScrollPane.setViewportView(itemDeHospedagemTable);
        itemDeHospedagemTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("itemDeHospedagemTable.columnModel.title0")); // NOI18N
        itemDeHospedagemTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("itemDeHospedagemTable.columnModel.title1")); // NOI18N
        itemDeHospedagemTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("itemDeHospedagemTable.columnModel.title2")); // NOI18N
        itemDeHospedagemTable.getColumnModel().getColumn(2).setCellRenderer(customCurrencyRenderer1);
        itemDeHospedagemTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("itemDeHospedagemTable.columnModel.title3")); // NOI18N

        javax.swing.GroupLayout itemDeHospedagemPanelLayout = new javax.swing.GroupLayout(itemDeHospedagemPanel);
        itemDeHospedagemPanel.setLayout(itemDeHospedagemPanelLayout);
        itemDeHospedagemPanelLayout.setHorizontalGroup(
            itemDeHospedagemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(itemDeHospedagemPanelLayout.createSequentialGroup()
                .addComponent(itemDeHospedagemScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE)
                .addGap(24, 24, 24))
        );
        itemDeHospedagemPanelLayout.setVerticalGroup(
            itemDeHospedagemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(itemDeHospedagemPanelLayout.createSequentialGroup()
                .addComponent(itemDeHospedagemScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getActionMap(HospedagemEditor.class, this);
        saveButton.setAction(actionMap.get("saveHospedagem")); // NOI18N
        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N

        cancelButton.setAction(actionMap.get("cancelHospedagem")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        jToolBar1.setBackground(resourceMap.getColor("jToolBar1.background")); // NOI18N
        jToolBar1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), resourceMap.getString("jToolBar1.border.title"), javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION)); // NOI18N
        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        newItemButton.setAction(actionMap.get("newItemDeHospedagem")); // NOI18N
        newItemButton.setText(resourceMap.getString("newItemButton.text")); // NOI18N
        newItemButton.setFocusable(false);
        newItemButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        newItemButton.setName("newItemButton"); // NOI18N
        newItemButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(newItemButton);

        editItemButton.setAction(actionMap.get("editItemDeHospedagem")); // NOI18N
        editItemButton.setText(resourceMap.getString("editItemButton.text")); // NOI18N
        editItemButton.setFocusable(false);
        editItemButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        editItemButton.setName("editItemButton"); // NOI18N
        editItemButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(editItemButton);

        deleteItemButton.setAction(actionMap.get("deleteItemDeHospedagem")); // NOI18N
        deleteItemButton.setText(resourceMap.getString("deleteItemButton.text")); // NOI18N
        deleteItemButton.setFocusable(false);
        deleteItemButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deleteItemButton.setName("deleteItemButton"); // NOI18N
        deleteItemButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(deleteItemButton);

        setFimHojeButton.setAction(actionMap.get("setFimHoje")); // NOI18N
        setFimHojeButton.setText(resourceMap.getString("setFimHojeButton.text")); // NOI18N
        setFimHojeButton.setName("setFimHojeButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(animalClienteEnderecoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 451, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(animalClienteNomeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 408, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(animalClienteFoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(animalClienteMobileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addComponent(animalComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(itemDeHospedagemPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(saveButton)
                        .addGap(18, 18, 18)
                        .addComponent(cancelButton)
                        .addGap(155, 155, 155)
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(inicioFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(fimFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(setFimHojeButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(animalComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(animalClienteNomeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(animalClienteEnderecoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(animalClienteFoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(animalClienteMobileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(inicioFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(fimFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(setFimHojeButton))
                .addGap(35, 35, 35)
                .addComponent(itemDeHospedagemPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(saveButton)
                        .addComponent(cancelButton))
                    .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                HospedagemEditor dialog = new HospedagemEditor(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void setFimHoje() {
        if(this.getCurrentRecord().getFaturamento() == null ||
                this.getCurrentRecord().getFaturamento().getTime()>0) {
            this.getCurrentRecord().setFim(new Date());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField animalClienteEnderecoTextField;
    private javax.swing.JTextField animalClienteFoneTextField;
    private javax.swing.JTextField animalClienteMobileTextField;
    private javax.swing.JTextField animalClienteNomeTextField;
    private javax.swing.JComboBox animalComboBox;
    private java.util.List<doghost.entities.Animal> animalList;
    private javax.persistence.Query animalQuery;
    private javax.swing.JButton cancelButton;
    private doghost.utils.CustomCurrencyRenderer customCurrencyRenderer1;
    private doghost.utils.CustomFormattedDateConverter customFormattedDateConverter1;
    private doghost.utils.CustomFormattedDateHMConverter customFormattedDateHMConverter1;
    private doghost.utils.CustomFormattedDateValidator customFormattedDateValidator1;
    private javax.swing.JButton deleteItemButton;
    private javax.persistence.EntityManager doghostPUEntityManager;
    private javax.swing.JButton editItemButton;
    private javax.swing.JFormattedTextField fimFormattedTextField;
    private javax.swing.JFormattedTextField inicioFormattedTextField;
    private org.jdesktop.swingx.JXPanel itemDeHospedagemPanel;
    private javax.swing.JScrollPane itemDeHospedagemScrollPane;
    private javax.swing.JTable itemDeHospedagemTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JToolBar jToolBar1;
    private javax.persistence.EntityManager localEntityManager;
    private javax.swing.JButton newItemButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton setFimHojeButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
